package com.yz.bi.service;

import com.yz.bi.mapper.BiMapper;
import com.yz.common.pojo.Database;
import com.yz.common.pojo.DbLink;
import com.yz.common.utils.JdbcUtils;
import com.yz.dba.mapper.DbaMapper;
import com.yz.dba.service.DbaService;
import com.yz.dba.vo.DatabaseInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 段成
 * @date 2019/12/12 - 10:07
 */
@Service
public class BiService {

    @Autowired
    private BiMapper biMapper;


    public List<DbLink> getLinkName() {
        return biMapper.getLinkName();
    }

    public List<Database> getDatabaseByLinkId(Integer linkId) {
        return biMapper.getDatabaseByLinkId(linkId);
    }

    public List<String> getTablesByDatabaseId(Integer databaseId) {
        String databaseName = biMapper.getDatabaseNameByDatabaseId(databaseId);
        return biMapper.getTablesByDatabaseName(databaseName);
    }

    public List<Map> getListByDatabaseNameAndTableName(Integer databaseId, String tableName,Integer pageNum) {
        Map map = this.generateDatabaseMap(databaseId);
        int size = 200;
        int pageIndex = (pageNum-1)*size;
        String databaseName = biMapper.getDatabaseNameByDatabaseId(databaseId);
        StringBuffer buffer = new StringBuffer();
        buffer.append("select * from ");
        buffer.append("`");
        buffer.append(databaseName);
        buffer.append("`.");
        buffer.append("`");
        buffer.append(tableName);
        buffer.append("`");
        buffer.append(" limit ");
        buffer.append(pageIndex);
        buffer.append(",");
        buffer.append(size);
        System.out.println(buffer.toString());

        try {
            List<Map> maps = JdbcUtils.queryData(map, buffer.toString());
            return maps;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public int getTotalByDatabaseNameAndTableName(Integer databaseId, String tableName) {
        Map map = this.generateDatabaseMap(databaseId);

        int count = JdbcUtils.queryCount(map, tableName);
        return count;

    }






    /**
     * 拼接url
     * @param databaseInfo
     * @return
     */
    public String spliceUrl(DatabaseInfo databaseInfo){
        StringBuffer url = new StringBuffer();
        if("mysql".equals(databaseInfo.getDbTypeName())){
            url.append("jdbc:mysql://");
            url.append(databaseInfo.getIp());
            url.append(":");
            url.append(databaseInfo.getPort());
            url.append("/");
            url.append(databaseInfo.getDatabaseName());
            url.append("?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=UTC");
        }
        if("Oracle".equals(databaseInfo.getDbTypeName())){
            //等待补全
        }
        return url.toString();
    }

    /**
     * 生成数据库map  其中调用了本类中的spliceUrl方法
     * @param databaseId
     * @return
     */
    public Map generateDatabaseMap(Integer databaseId){

        DatabaseInfo databaseInfo = biMapper.getDatabaseInfo(databaseId);
        String url = this.spliceUrl(databaseInfo);


        HashMap<String, String> map = new HashMap<>();
        map.put("driver", databaseInfo.getDriver());
        map.put("url", url);
        map.put("username", databaseInfo.getUsername());
        map.put("pass", databaseInfo.getPass());
        return map;
    }


}
